/*
 * @Author: zhang xinzheng
 * @Date: 2022-01-20 14:30:56
 * @LastEditors: zhang xinzheng
 * @LastEditTime: 2022-01-20 17:21:08
 * @Description: 
 * @FilePath: \report-service\src\Charts\AxisChart.ts
 */
import DataSource, { DataSourceMgr } from "../dataSource/dataSource"
import Charts, { IChartConfig, ChartConfigMgr } from "./Charts"
import { EChartType, EAxisChartDimension } from "../core/ChartType"
export enum EAxisType {
    Category = 'category',
    Value = "value",
    Time = 'time',
    Log = 'log'
}
export interface IAxiosChartOptions {

    xAxis: {
        type: EAxisType,
        data: Array<string>
    },
    yAxis: {
        type: EAxisType
    },
    series: Array<{
        data: Array<number>,
        type: EAxisChartDimension
        areaStyle?: any
    }>
}
export interface IAxisChartConfig extends IChartConfig {
    ChartOptions: IAxiosChartOptions
}
export default class AxisChart extends Charts<IAxisChartConfig>{
    constructor(config: { [key: string]: any }, dataSource: DataSourceMgr, id?: string) {
        super(config, dataSource, id)
    }
    computedConifgStrategy(data: any) {
        let options = <Map<keyof IAxisChartConfig, any>>this.getConfig();
        const dataLength = options.get("limit") || 0;
        if (dataLength) {
            data = data.slice(0, dataLength);
        }
        let axisConfig = options.get("chartOptions");
        if (!axisConfig.xAxis)
            return;
        try {
            if (typeof axisConfig.xAxis.data == "string") {
                const xAxisConfFn = new Function("$originData", axisConfig.xAxis.data);
                axisConfig.xAxis.data = xAxisConfFn(data);
            }
            if (typeof axisConfig.series == "string") {
                const seriesConfFn = new Function("$originData", axisConfig.series);
                console.log("seriesConfFn", seriesConfFn);
                axisConfig.series = seriesConfFn(data);
            }

            this.setConfig("chartOptions", axisConfig)
        } catch (error) {
            console.error(error)
        }
    }
}